<template>
  <div class="white-box">
    <div class="table-mb" v-if="new_house_dong_id">
      <el-button type="primary" @click="handleAdd({new_house_dong_id:new_house_dong_id})">添加户型</el-button>
    </div>
    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">户型名称：</span>
        <el-input placeholder="请输入户型名称" v-model="searchParams.title" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">出售状态：</span>
        <el-select v-model="searchParams.sell_status" placeholder="请选择出售状态" clearable>
          <el-option label="待售" :value="1"/>
          <el-option label="在售" :value="2"/>
          <el-option label="售馨" :value="3"/>
        </el-select>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">上架状态：</span>
        <el-select v-model="searchParams.shangjia_status" placeholder="请选择上架状态" clearable>
          <el-option label="上架" :value="1"/>
          <el-option label="下架" :value="2"/>
        </el-select>
      </div>
      <div class="task-one__btn clearfix">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)">
      <el-table-column prop="pan_title" label="所属楼盘"> </el-table-column>
      <el-table-column prop="dong_title" label="所属栋"> </el-table-column>
      <el-table-column prop="title" label="户型名称"></el-table-column>
      <el-table-column prop="danyuan_num" label="户型">
        <template #default="{ row }">
          <div>{{row.shi_num}}室{{row.ting_num}}厅{{row.wei_num}}卫</div>
        </template>
      </el-table-column>
      <el-table-column prop="area_num" label="建筑面积（平米）"></el-table-column>
      <el-table-column prop="zhuangxiu_label" label="房源标签(亮点)"></el-table-column>
      <el-table-column prop="sell_status" label="户型图">
        <template #default="{ row }">
          <el-image
              style="width: 100px; height: 100px"
              :src="proxy.imgUrl + row.img1[0]"
              fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column prop="sell_status" label="出售状态" width="80">
        <template #default="{ row }">
          <el-text v-if="row.sell_status==1" type="success">{{row.sell_status_label}}</el-text>
          <el-text v-if="row.sell_status==2" type="danger">{{row.sell_status_label}}</el-text>
          <el-text v-if="row.sell_status==3" type="info">{{row.sell_status_label}}</el-text>
        </template>
      </el-table-column>
      <el-table-column prop="shangjia_status" label="上架状态" width="150">
        <template #default="{ row }">
          <el-radio-group v-model="row.shangjia_status" size="small" @change="shangjia_status_change(row,$event)">
            <el-radio-button :label="1">上架</el-radio-button>
            <el-radio-button :label="2">下架</el-radio-button>
          </el-radio-group>
        </template>
      </el-table-column>

<!--      <el-table-column prop="created_at" label="创建时间"> </el-table-column>-->
      <el-table-column label="操作" width="140" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit(row,{new_house_hu_id:row.new_house_hu_id,new_house_dong_id:row.new_house_dong_id})">编辑</el-button>
          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                         @confirm="handleDelete(row)">
            <template #reference>
              <el-button link type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="pageSizeNum"
                   :total="totalNums" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="1000px" :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px" class="houseNewHu-form">

        <el-row :gutter="0">
          <el-col :span="12">
            <el-form-item label="户型名称" prop="title">
              <el-input v-model.trim="dialogForm.title" clearable placeholder="请输入户型名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建筑面积" prop="area_num">
              <el-input v-model.trim="dialogForm.area_num" clearable placeholder="请输入建筑面积" oninput="value=value.replace(/[^\d.]/g,'')">
                <template #append>平米</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="X室" prop="shi_num">
              <el-input v-model.trim="dialogForm.shi_num" clearable placeholder="请输入整数" oninput="value=value.replace(/[^\d]/g,'')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="X厅" prop="ting_num">
              <el-input v-model.trim="dialogForm.ting_num" clearable placeholder="请输入整数" oninput="value=value.replace(/[^\d]/g,'')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="X卫" prop="wei_num">
              <el-input v-model.trim="dialogForm.wei_num" clearable placeholder="请输入整数" oninput="value=value.replace(/[^\d]/g,'')"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="户型标签" prop="trait_id">
          <el-select v-model="dialogForm.trait_id" placeholder="请选择房源装修" multiple>
            <el-option :label="item.label" :value="item.value" v-for="(item, index) in selectOptions.trait_id"
                       :key="index"/>
          </el-select>
        </el-form-item>
        <el-row :gutter="0">
          <el-col :span="12">
            <el-form-item label="出售状态" prop="sell_status">
              <el-radio-group v-model="dialogForm.sell_status">
                <el-radio :label="1">待售</el-radio>
                <el-radio :label="2">在售</el-radio>
                <el-radio :label="3">售馨</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上架状态" prop="shangjia_status">
              <el-radio-group v-model="dialogForm.shangjia_status">
                <el-radio :label="1">上架</el-radio>
                <el-radio :label="2">下架</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="户型图" prop="img1">
          <File-Oss-upload v-model="dialogForm.img1" :fileSize="50" :limit="9" :fileType="['png', 'jpg', 'jpeg']" listType="picture-card" modelValueType="Array"></File-Oss-upload>
        </el-form-item>
        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm(dialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch
} from "vue"
import listMixins from "@mixins/list"
import {
  useRoute,
  useRouter
} from "vue-router"
import {
  Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"
import {EluiChinaAreaDht} from "elui-china-area-dht";
import {validNumOrLetter} from "@common/utils";
import FileOssUpload from "@/components/FileOssUpload.vue"

const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  handleEdit,
  resetDialogForm,
  handleAllSelected,
  submitDialogForm,
  closeDialog,
  handleAction
} = listMixins()

const router = useRouter()
const route = useRoute()

const new_house_dong_id = route.query.new_house_dong_id || ''
searchParams.new_house_dong_id = new_house_dong_id

// new_house_dong_id
dialogForm.title = '';
dialogForm.sell_status = 1;
dialogForm.shangjia_status = 1;
dialogForm.shi_num = '';
dialogForm.ting_num = '';
dialogForm.wei_num = '';
dialogForm.danyuan_num = '';
dialogForm.trait_id = [];
dialogForm.img1 = [];
dialogForm.area_num = '';

apiPath.value = 'admin/house/new/hu/index';
dialogParams.addApiPath = 'admin/house/new/hu/create';
dialogParams.editApiPath = 'admin/house/new/hu/update';

onMounted(() => {
  getData()
  getSelectOptions()
})

const dialogFormRules = reactive({
  title:[{
    required: true,
    message: "请输入户型名称",
    trigger: "blur"
  }],
  trait_id: [{
    required: true,
    message: "请选择户型标签(亮点)",
    trigger: "change"
  }],
  sell_status: [{
    required: true,
    message: "请选择出售状态",
    trigger: "change"
  }],
  shangjia_status: [{
    required: true,
    message: "请选择上架状态",
    trigger: "change"
  }],
  shi_num:[{
    required: true,
    message: "请输入整数",
    trigger: "blur"
  }],
  ting_num:[{
    required: true,
    message: "请输入整数",
    trigger: "blur"
  }],
  wei_num:[{
    required: true,
    message: "请输入整数",
    trigger: "blur"
  }],
  min_area_num: [{required: true, message: "请输入建筑面积", trigger: "blur"},{validator:proxy.$util.validNumOrPoint,trigger: "blur"}],
})
const getImgs = (list) =>{
  return list.map((item)=>proxy.imgUrl + item)
}
const shangjia_status_change = (row, e) => {
  handleAction('admin/house/new/hu/changeShangJiaStatus', {
    new_house_hu_id: row.new_house_hu_id,
    shangjia_status: e
  })
}

const handleDelete=(row)=> {
  handleAction('admin/house/new/hu/delete', {
    new_house_hu_id: row.new_house_hu_id,
  })
}

const selectOptionsConfig = {
  trait_id:{url:'common/dataSelect/houseTagSelect',params:{p_id:8}}
}
const selectOptions = reactive({
  trait_id:[]
})
const getSelectOptions = () => {
  for (let key in selectOptionsConfig){
    let {url,params} = selectOptionsConfig[key]
    proxy.$axios
        .get(url,{params:params})
        .then((res) => {
          if (res.data.code == 200) {
            let arr;
            if(url == 'common/dataSelect/houseTagSelect'){
              arr = res.data.data.map(item => {
                return {
                  label:item.house_tag_title,
                  value:item.house_tag_id,
                }
              })
            }
            selectOptions[key] = arr
          } else {
            proxy.$message({
              message: res.data.msg,
              type: "error"
            })
          }
        })
        .catch((err) => {
        })
  }
}

</script>

<style lang="less">
.houseNewHu-form .el-select  .el-input{
  width: 700px;
}
</style>